import json

import requests
from flask import Blueprint, current_app, render_template

from applications.common.utils.http import success_api, fail_api

map_bp = Blueprint('map', __name__, url_prefix='/police/map')


@map_bp.get('/<int:id>')
def locate(id):
    response = requests.post(url=current_app.config['EMERGENCY_URL'] + '/pos/gps',
                             headers={
                                 'Content-type': 'application/json'
                             },
                             data=json.dumps({'id': id}))
    res_json = json.loads(response.text)

    if res_json['code'] != 200:
        return fail_api('获取定位失败')

    data = res_json['data']
    longitude = data['lon']
    latitude = data['lat']

    return render_template('admin/map/map.html', longitude=longitude, latitude=latitude)
